﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Globalization;



public partial class Kho_UserControl_uc_SoanHang_ChiTiet : System.Web.UI.UserControl
{
    public int _ID;
    public DateTime _Ngay;
    public int _BuoiID;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            InitData();
        }
    }

    private void InitData()
    {

        lblAlerting.Text = string.Empty;
        hdfDateTime.Value = _Ngay.ToString("dd/MM/yyyy");
        hdfBuoiID.Value = _BuoiID.ToString();
        hdfID.Value = _ID.ToString();
        lblNgay.Text = _Ngay.ToString("dd/MM/yyyy");
        lblBuoi.Text = GetBuoiName();        
        CheckTonTai();
        GetDelivery();
    }
    private void CheckTonTai()
    {
        DeliveryBO del = new DeliveryBO();

        int rs = del.Delivery_SO_Kho_Check_New(int.Parse(hdfID.Value));
        if (rs > 0)
        {
           btnSave.Visible = true;
        }
        else
        {
            btnSave.Visible = false;
        }

    }
    private void GetDelivery()
    {
        DeliveryBO del = new DeliveryBO();

        List<ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult> result = new List<ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult>();
        result = del.Delivery_SO_Detail_Kho_View_Detail(int.Parse(hdfID.Value));
        grdList.DataSource = result;
        grdList.DataBind();
        if (grdList.Rows.Count > 0)
        {
            //This replaces <td> with <th> and adds the scope attribute
            grdList.UseAccessibleHeader = true;

            //This will add the <thead> and <tbody> elements
            grdList.HeaderRow.TableSection = TableRowSection.TableHeader;

            //This adds the <tfoot> element. 
            //Remove if you don't have a footer row
            //gvTheGrid.FooterRow.TableSection = TableRowSection.TableFooter;
        }

    }
    private string GetBuoiName()
    {
        CateRouteBO cate = new CateRouteBO();

        ZD_PRC_ZD_CATE_BUOI_GETBYIDResult result = new ZD_PRC_ZD_CATE_BUOI_GETBYIDResult();
        result = cate.BuoiGet_SingleLine(int.Parse(hdfBuoiID.Value));
        return result.TENBUOI;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
       
            try
            {
                lblAlerting.Text = string.Empty;
                bool isSave = true;
                bool check = false;

                DateTime dtSearch = DateTime.ParseExact(lblNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);

                for (int i = 0; i < grdList.Rows.Count; i++)
                {
                    // Cell 9 la checkbox, cell 10 la textbox
                    CheckBox chkSave = ((CheckBox)grdList.Rows[i].Cells[9].FindControl("chkSave"));
                    TextBox txtSOLUONG = ((TextBox)grdList.Rows[i].Cells[10].FindControl("txtSOLUONG"));

                    if ((chkSave.Checked) && int.Parse(txtSOLUONG.Text.Trim().Replace(",","")) > 0)
                    {
                        check = true;
                        break;
                    }
                }
                if (!check)
                {
                    lblAlerting.Text = "Không có dữ liệu lập phiếu soạn!";
                    return;
                }
                //   
                try
                {

                    DeliveryBO del = new DeliveryBO();

                    for (int i = 0; i < grdList.Rows.Count; i++)
                    {
                        // Cell 9 la checkbox, cell 10 la textbox
                        CheckBox chkSave = ((CheckBox)grdList.Rows[i].Cells[8].FindControl("chkSave"));
                        string strTSL = ((Label)grdList.Rows[i].Cells[7].FindControl("lblListingLenhGiaoHang")).Text.Trim();
                        string strSoLuong = ((TextBox)grdList.Rows[i].Cells[9].FindControl("txtSOLUONG")).Text.Trim().Replace(",", "");
                        string strID = ((HiddenField)grdList.Rows[i].Cells[0].FindControl("hdfID")).Value;

                        if ((chkSave.Checked) && int.Parse(strSoLuong) > 0)
                        {
                            if (int.Parse(strTSL) >= int.Parse(strSoLuong))
                            {
                                int rs = del.Delivery_SO_Detail_Kho_Save(int.Parse(strID), int.Parse(Session["UserID"].ToString()), chkSave.Checked, int.Parse(strSoLuong));
                                if (rs <= 0)
                                {
                                    isSave = false;
                                }
                            }

                        }
                    }
                    if (!isSave)
                    {
                        lblAlerting.Text = "Lưu thất bại, bạn vui lòng thử lại!";
                        return;
                    }
                    else
                    {
                        btnSave.Visible = false;                      
                        lblAlerting.Text = "Lưu thành công!";
                    }
                }
                catch
                {

                    lblAlerting.Text = "Lưu thất bại, bạn vui lòng thử lại!";
                    return;
                }

            }
            catch
            {


            }
    }
    private DataTable ConvertToDataTable<ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult>(List<ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }
    protected void btnXuat_Click(object sender, EventArgs e)
    {
        OutReport outRPT = new OutReport();
        //lay database
        DeliveryBO del = new DeliveryBO();
        List<ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult> result = new List<ZD_PRC_ZD_DELIVERY_KHO_VIEW_DETAILResult>();
        result = del.Delivery_SO_Detail_Kho_View_Detail(int.Parse(hdfID.Value));
        DataTable dt1 = ConvertToDataTable(result);
        DataTable dt2 = new DataTable();
        DataColumn column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "DateReport";
        dt2.Columns.Add(column);
        DataColumn column2 = new DataColumn();
        column2.DataType = System.Type.GetType("System.String");
        column2.ColumnName = "Buoi";
        dt2.Columns.Add(column2);
        DataRow row = dt2.NewRow();
        row["DateReport"] = lblNgay.Text.Trim();
        row["Buoi"] = lblBuoi.Text.Trim();
        dt2.Rows.Add(row);
        // datatable 1
        DataTable Report1 = new DataTable();
        // datatable 2
        DataTable Report2 = new DataTable();
        Report1 = dt1;
        Report2 = dt2;
        Report1.TableName = "Report";
        Report2.TableName = "Date";
        DataTable[] arrTable = { Report1, Report2 };
        outRPT.OutReportToScreen(MapPath("~/RPTTemplate/RPT_PXUATKHO_TEMPLATE.xls"), arrTable, "_PXuatKhoReport.xls");
    }
}
